/**
 * 水印相关的方法
 */

 let setWatermark = (str1, str2,font,color,domId,apId) => {
  let id = domId || '1.23452384164.123412415';
  let appendDom  = document.getElementById(apId)
  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id))
  }
  let can = document.createElement('canvas')
  // 设置canvas画布大小
  can.width = 150
  can.height = 80
  let cans = can.getContext('2d')
  cans.rotate(-20 * Math.PI / 180) // 水印旋转角度
  cans.font = font || '15px Vedana'
  cans.fillStyle =color|| '#15F6DA'
  cans.textAlign = 'center'
  cans.textBaseline = 'Middle'
  cans.fillText(str1, can.width / 2-40, can.height-30) // 水印在画布的位置x，y轴
  cans.fillText(str2, can.width / 2-40, can.height + 22-30)
  let div = document.createElement('div')
  div.id = id
  div.style.pointerEvents = 'none'
  div.style.top = '0px'
  div.style.left = '0px'
  div.style.opacity = '0.5'
  div.style.position = 'absolute'
  div.style.overflow = 'hidden'
  div.style.zIndex = '1'
  if(appendDom){
    // 全屏展示
    div.style.width = appendDom.offsetWidth + 'px'
    div.style.height = appendDom.offsetHeight + 'px'
  }else{
    // 非全屏展示
    div.style.width = document.documentElement.clientWidth + 'px'
    div.style.height = document.documentElement.clientHeight  + 'px'
  }
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  if(appendDom){
    appendDom.appendChild(div)
  }else{
    document.body.appendChild(div)
  }
  return id
}

/**
 * 添加水印方法
 * @param str1 // 水印字符串
 * @param str2 // 水印字符串
 * @param font // 字体大小
 * @param color // 字体颜色
 * @param domId // 生成的dom元素
 * @param apId  // 如果是相对的生成元素下的ID
 */
const dmSetWaterMark = (str1, str2,font,color,domId,apId) => {
  let id = setWatermark(str1, str2,font,color,domId,apId)
  if (document.getElementById(id) === null) {
    id = setWatermark(str1, str2,font,color,domId,apId)
  }
}

/**
 * 移除水印方法
 * @param domId
 */
const dmRemoveWatermark = (domId) => {
  let id =domId || '1.23452384164.123412415'
  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id))
  }
}

export {
  dmSetWaterMark,
  dmRemoveWatermark
}
